Conditions | 1 |
Paths | 1 |
Total Lines | 157 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import chai from 'chai'; |
||
16 | describe(`[module] configure`, function() { |
||
17 | describe(`[method] load`, function() { |
||
18 | let defaultconfpath = `.codingamerc`; |
||
19 | let incorrectconfpath = `.codingamerc.txt`; |
||
20 | let defaultconf = { |
||
21 | "username": `me`, |
||
22 | "password": `somepass`, |
||
23 | "exercise": `5711567e959cf54dd2dd79c1b4c259560d6ba46`, |
||
24 | "tests": [1, 2], |
||
25 | "language": `Python`, |
||
26 | "bundle": `bundle.py` |
||
27 | }; |
||
28 | before(function() { |
||
29 | mockfs({ |
||
30 | [defaultconfpath]: JSON.stringify(defaultconf), |
||
31 | [incorrectconfpath]: `This is not valid JSON!` |
||
32 | }); |
||
33 | }); |
||
34 | after(function() { |
||
35 | mockfs.restore(); |
||
36 | }); |
||
37 | afterEach(function() { |
||
38 | for (let param in defaultconf) { |
||
39 | configure.forget(param); |
||
40 | } |
||
41 | }); |
||
42 | |||
43 | it(`should return the content of file parsed as JSON`, function() { |
||
44 | let conf = configure.load(defaultconfpath); |
||
45 | return expect(conf).to.eventually.be.deep.equal(defaultconf); |
||
46 | }); |
||
47 | |||
48 | it(`should reject because of incorrect file path`, function() { |
||
49 | let conf = configure.load(`/incorrect/path/.codingamerc`); |
||
50 | return expect(conf).to.eventually.be.rejected; |
||
51 | }); |
||
52 | |||
53 | it(`should reject because of incorrect parsing of JSON`, function() { |
||
54 | let conf = configure.load(incorrectconfpath); |
||
55 | return expect(conf).to.eventually.be.rejected; |
||
56 | }) |
||
57 | |||
58 | it(`should return options if path is incorrect and options is defined`, function() { |
||
59 | let conf = configure.load(undefined, defaultconf); |
||
60 | return expect(conf).to.eventually.be.deep.equal(defaultconf); |
||
61 | }); |
||
62 | }); |
||
63 | describe(`[method] get`, function() { |
||
64 | let filepath = `file.txt`; |
||
65 | let filecontent = `Hello world!` |
||
66 | let shellcmd = [`echo`, filecontent]; |
||
67 | let defaultconf = { |
||
68 | "shell": shellcmd, |
||
69 | "wrongshell": [`notacommand`], |
||
70 | "path": filepath, |
||
71 | "notapath": `/not/a/path` |
||
72 | }; |
||
73 | let answer = `42`; |
||
74 | let createInterface; |
||
75 | before(function(done) { |
||
76 | mockfs({ |
||
77 | [filepath]: filecontent |
||
78 | }); |
||
79 | configure.load(undefined, defaultconf).then(function() { |
||
80 | done(); |
||
81 | }) |
||
82 | }); |
||
83 | after(function() { |
||
84 | mockfs.restore(); |
||
85 | for (let param in defaultconf) { |
||
86 | configure.forget(param); |
||
87 | } |
||
88 | }); |
||
89 | let sandbox; |
||
90 | beforeEach(function() { |
||
91 | sandbox = sinon.sandbox.create() |
||
92 | createInterface = sandbox.stub(readline, 'createInterface', function() { |
||
93 | return { |
||
94 | "question": function(question, cb) { |
||
95 | cb(answer); |
||
96 | }, |
||
97 | "close": function() {} |
||
98 | }; |
||
99 | }); |
||
100 | }); |
||
101 | afterEach(function() { |
||
102 | sandbox.restore(); |
||
103 | }); |
||
104 | it(`should return the string property as it is`, function() { |
||
105 | let get = configure.get(`path`); |
||
106 | return expect(get).to.eventually.be.equal(filepath); |
||
107 | }); |
||
108 | it(`should return the array property as it is`, function() { |
||
109 | let get = configure.get(`shell`); |
||
110 | return expect(get).to.eventually.be.deep.equal(shellcmd); |
||
111 | }); |
||
112 | it(`should return the result of the shell command`, function() { |
||
113 | let get = configure.get(`shell`, `shell`); |
||
114 | return expect(get).to.eventually.be.deep.equal(filecontent); |
||
115 | }); |
||
116 | it(`should return the content of the file`, function() { |
||
117 | let get = configure.get(`path`, `file`); |
||
118 | return expect(get).to.eventually.be.deep.equal({ |
||
119 | "path": filepath, |
||
120 | "data": filecontent |
||
121 | }); |
||
122 | }); |
||
123 | it(`should return answer to the question`, function() { |
||
124 | let ask = `ask something?`; |
||
125 | let get = configure.get(`questionproperty`, ``, ask); |
||
126 | expect(createInterface).to.be.calledOnce; |
||
|
|||
127 | return expect(get).to.eventually.be.equal(answer); |
||
128 | }); |
||
129 | it(`should reject because property doesn't exist`, function() { |
||
130 | let get = configure.get(`notaproperty`); |
||
131 | return expect(get).to.eventually.be.rejected; |
||
132 | }); |
||
133 | it(`should reject if first parameter is not a string`, function() { |
||
134 | let get = configure.get(42); |
||
135 | return expect(get).to.eventually.be.rejected; |
||
136 | }); |
||
137 | it(`should reject if shell command is failing`, function() { |
||
138 | let get = configure.get(`wrongshell`, `shell`); |
||
139 | return expect(get).to.eventually.be.rejected; |
||
140 | }); |
||
141 | it(`should reject if file doesn't exist`, function() { |
||
142 | let get = configure.get(`notapath`, `file`); |
||
143 | return expect(get).to.eventually.be.rejected; |
||
144 | }); |
||
145 | it(`should reject if question is not a string`, function() { |
||
146 | let get = configure.get(`incorrectquestionproperty`, ``, 42); |
||
147 | expect(createInterface).to.be.calledOnce; |
||
148 | return expect(get).to.eventually.be.rejected; |
||
149 | }); |
||
150 | }); |
||
151 | describe(`[method] forget`, function() { |
||
152 | let conf = { |
||
153 | "property": `value` |
||
154 | }; |
||
155 | before(function() { |
||
156 | configure.load(undefined, conf); |
||
157 | }) |
||
158 | it(`should forget about the parameter`, function() { |
||
159 | configure.forget(`property`); |
||
160 | let get = configure.get(`property`); |
||
161 | return expect(get).to.eventually.be.rejected; |
||
162 | }); |
||
163 | it(`should throw an error when the property is not a string`, function() { |
||
164 | try { |
||
165 | configure.forget(null); |
||
166 | } catch(error) { |
||
167 | return expect(error).to.be.an('error'); |
||
168 | } |
||
169 | return expect(false).to.be.ok; |
||
170 | }); |
||
171 | }); |
||
172 | }); |
||
173 |